/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package AdminCenterMain;

import CommonValue.SystemTimer;
import DTO.ControlDTO;
import MyLogging.ILog;
import SocketBusiness.ServerListen;
import DTO.PacketDTO;
import DTO.SensorDTO;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author Administrator
 */
public class ReceiveTriggerFromAS extends ServerListen {

    private PacketDTO _trigger;

    public ReceiveTriggerFromAS(String systemSign, int port, ControlDTO operation, PacketDTO _trigger) {
        super(systemSign, port, operation);
        this._trigger = _trigger;
    }

    @Override
    public void business() {
        boolean finish = false;
        do {
            try {
                Thread.sleep(SystemTimer.CheckTriggerSleep);
            } catch (InterruptedException ex) {
                Logger.getLogger(ReceiveTriggerFromAS.class.getName()).log(Level.SEVERE, null, ex);
            }
            
            if (!this.getOperation().getFlag()) {
                System.out.println(this.getSystemSign() + " DA DUNG HOAT DONG <<<<<<<<<<<<<<<<");
                continue;
            }
            Object obj = new Object();
            try {
                obj = this.receiveMessage();
            } catch (ClassNotFoundException ex) {
                Logger.getLogger(ReceiveTriggerFromAS.class.getName()).log(Level.SEVERE, null, ex);
            }
            if (obj == null) {
                System.out.println("TriggerDTO is null, everything is ok");
                continue;
            }
            if (!obj.getClass().equals(PacketDTO.class)) {
                System.out.println("Invalid Type Class");
                continue;
            }

            ILog.writeLog(this.getClass() + "Da nhan duoc packet", this.getSystemSign() + "--------------- Thoi tiet bat thuong : ");
            synchronized (_trigger) {
                _trigger.clearBuffer();
                _trigger.setBuffer(((PacketDTO) obj).getBuffer());
                System.out.println("so khoi du lieu trong packet la :  " + _trigger.getBuffer().size());
            }

            for (SensorDTO i : ((PacketDTO) obj).getBuffer()) {
                System.out.println(i.getSensorSign());
                i.printData();
            }

        } while (!finish);
    }
}
